package com.edu.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import org.w3c.dom.css.Rect;


import com.edu.model.Piece;
import com.edu.model.Position;
import com.edu.view.ChessView;

public class ChessHelper {
	private ChessHelper() {
	}

	public static void setStrokeWidth(Graphics2D g2d, int strokeWidth) {
		g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
	}

	public static void strokeCircle(Graphics2D g2d, Point center, int r) {
		Ellipse2D cilcle = new Ellipse2D.Double(center.x - r, center.y - r, 2 * r, 2 * r);
		g2d.draw(cilcle);
	}

	public static void fillCircle(Graphics2D g2d, Point center, int r) {
		Ellipse2D cilcle = new Ellipse2D.Double(center.x - r, center.y - r, 2 * r, 2 * r);
		g2d.fill(cilcle);
	}

	public static void setColor(Graphics2D g2d, Color color) {
		g2d.setColor(color);
	}

	public static void drawLine(Graphics2D g2d, Point src, Point dst) {
		g2d.drawLine(src.x, src.y, dst.x, dst.y);
	}

	public static void drawText(Graphics2D g2d, String text, Point center) {
		Font font = new Font("宋体", Font.PLAIN, 28);
		g2d.setFont(font);
		FontMetrics fm = g2d.getFontMetrics(font);
		int textWidth = fm.stringWidth(text);
		g2d.drawString(text, center.x - textWidth / 2, center.y + textWidth / 2 - 4);
	}

	// 绘制棋子
	public static void drawPiece(Graphics2D g2d, Piece piece ,String selectedPieceKey , int r ,int length) {
		
		setColor(g2d, piece.getColor());
		setStrokeWidth(g2d, 5);
		Point center = getCenterPoint(piece.getPosition(), length);
		strokeCircle(g2d, center, r);
		Color color = selectedPieceKey == piece.getKey()  ? Color.GRAY : Color.WHITE;
		setColor(g2d, color);
		fillCircle(g2d, center, r);
		setColor(g2d, piece.getColor());
		drawText(g2d, piece.getName(), center);
		setColor(g2d, Color.BLACK);
	}


	public static Point getCenterPoint(int row, int col , int length) {
		return new Point(row * length, col * length);
	}
	public static Point getCenterPoint(Position pos , int length) {
		return getCenterPoint(pos.row + 1, pos.col+1, length);
	}

	// 获取点击坐标的行列值(效率不高,最好用除法)
	public static Position getRowCol(Point pt , int length) {
		Position pos = null;
		for (int row = 0; row < 9; row++) {
			for (int col = 0; col < 10; col++) {
				Point c = getCenterPoint(row, col, length);
				int dx = c.x - pt.x;
				int dy = c.y - pt.y;
				int dist = dx * dx + dy * dy;
				if (dist * 4 < length * length) {
					pos = new Position(row, col);
				}
			}
		}
		return pos;
	}
	//吃子的走法
	public static boolean canMovebyEat(Piece src, Piece killed , String selectedPieceKey) {
		//如果颜色相同
		if (src.getColor() == killed.getColor()) {
			//1.换选择的棋子
			//src.setSelected(false);
			//killed.setSelected(true);
			return false;
		} 
		//switch (src.getType()) {
		
		//}
		
		return true;
	}
	
	public static boolean canMove(Piece src, int row ,int col) {
		
		return false;
	}

	
}
